package fina2.dcs.returns.validator;

import java.io.Serializable;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.naming.InitialContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;

import fina2.dcs.context.ContextHelper;
import fina2.dcs.returns.ReturnVersionLocal;

@Name("returnVersionValidator")
@Scope(ScopeType.SESSION)
public class ReturnVersionValidator implements Serializable {

	private InitialContext ic = null;

	private ReturnVersionLocal rvl = null;

	private ResourceBundle bundle = null;

	private String oldCode = null;

	private Log log = Logging.getLog(getClass());

	public ReturnVersionValidator() {
		try {
			ic = ContextHelper.getInitialContext();
			rvl = ContextHelper.injectRetVersionLocal(ic);
			UIViewRoot ui = FacesContext.getCurrentInstance().getViewRoot();
			Locale l = ui.getLocale();
			ClassLoader cl = Thread.currentThread().getContextClassLoader();
			bundle = ResourceBundle.getBundle("messages", l, cl);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
	}

	public void validateCode(FacesContext fc, UIComponent ui, Object value) throws ValidatorException {
		System.out.println("CODE " + value);
		String val = value.toString().trim();
		if (val.length() < 1 || val.length() > 12) {
			FacesMessage fm = new FacesMessage();
			fm.setSeverity(FacesMessage.SEVERITY_ERROR);
			fm.setDetail(bundle.getString("fina2.web.illegal.code.length"));
			throw new ValidatorException(fm);
		} else if (rvl.findByCode(val) != null) {
			FacesMessage fm = new FacesMessage();
			fm.setSeverity(FacesMessage.SEVERITY_ERROR);
			fm.setDetail(bundle.getString("fina2.returns.returnVersionCodeNotUnique"));
			throw new ValidatorException(fm);
		}
	}

	public void validateEditCode(FacesContext fc, UIComponent ui, Object value) {
		String val = value.toString().trim();
		if (!val.equals(oldCode.trim())) {
			if (val.length() < 1 || val.length() > 12) {
				FacesMessage fm = new FacesMessage();
				fm.setSeverity(FacesMessage.SEVERITY_ERROR);
				fm.setDetail(bundle.getString("fina2.web.illegal.code.length"));
				throw new ValidatorException(fm);
			}
			else if (rvl.findByCode(val) != null) {
				FacesMessage fm = new FacesMessage();
				fm.setSeverity(FacesMessage.SEVERITY_ERROR);
				fm.setDetail(bundle.getString("fina2.returns.returnVersionCodeNotUnique"));
				throw new ValidatorException(fm);
			}
		}
	}

	public void validateDescription(FacesContext fc, UIComponent ui, Object value) throws ValidatorException {
		String val = value.toString().trim();
		if (val.length() < 1 || val.length() > 255) {
			FacesMessage fm = new FacesMessage();
			fm.setSeverity(FacesMessage.SEVERITY_ERROR);
			fm.setDetail(bundle.getString("fina2.web.illegal.description.length"));
			throw new ValidatorException(fm);
		}
	}

	public String getOldCode() {
		return oldCode;
	}

	public void setOldCode(String oldCode) {
		this.oldCode = oldCode;
	}

}
